package deanandjames.domain;

import deanandjames.configuration.Configuration;
import deanandjames.movement.Moveable;
import deanandjames.movement.MovementManager;
import deanandjames.shapes.BoundingBox;

import javax.vecmath.Point2d;
import java.awt.*;

public class SpaceShip implements Controlable, Boundable, Moveable {
	private final Color colour = Color.green;
	private double direction;
	private double heading;
	private final Laser laser;
	private final MovementManager movementManager;
	private final int panelWidth;
	private final PolygonGenerator polygonGenerator = new PolygonGenerator();
	private final Point2d position;
	private int shotsFired;
	private double speed;

	public SpaceShip(int panelWidth, int panelHeight) {
		this.panelWidth = panelWidth;
		position = new Point2d(0.5 * panelWidth, 0.5 * panelHeight);
		speed = 0;
		heading = 0.0;
		direction = heading;
		movementManager = new MovementManager(this, panelWidth, panelHeight, true);
		laser = new Laser(new Point2d(position.x, position.y), heading, panelWidth, panelHeight);
		laser.disable();
		shotsFired = 0;
	}

	public void accellerate() {
		movementManager.accellerate();
	}

	public void animate() {
		movementManager.moveAndScreenWrap();
	}

	public void decellerate() {
		movementManager.decellerate();
	}

	public void draw(Graphics2D graphics) {
		Polygon polygon = polygonGenerator.generate(position, heading, 3, 10);

		if (Configuration.getInstance().isDebugEnabled()) {
			graphics.setColor(Color.yellow);
			graphics.drawString(shotsFired + " shots fired", panelWidth - 100, 50);

			graphics.setColor(Color.blue);
			graphics.draw(getBoundingBox().getRectangle());

			graphics.setColor(Color.darkGray);
			graphics.drawString((int) position.x + "," + (int) position.y + ":" + heading, (int) position.x, (int) position.y + 20);

			graphics.setColor(Color.yellow);
			graphics.drawLine((int) position.x, (int) position.y, (int) (Math.cos(heading) * 50 + position.x), (int) (Math.sin(heading) * 50 + position.y));

			graphics.setColor(Color.red);
			graphics.drawLine((int) position.x, (int) position.y, (int) (Math.cos(direction) * 50 + position.x), (int) (Math.sin(direction) * 50 + position.y));
		}

		graphics.setColor(colour);
		graphics.drawPolygon(polygon);

		graphics.setColor(Color.red);
		graphics.drawOval(polygon.xpoints[0], polygon.ypoints[0], 1, 1);

		if (laser != null) {
			laser.draw(graphics);
		}

	}

	public Laser fireLaser() {
		shotsFired++;
		laser.reset(getPosition(), heading);
		return laser;
	}

	public BoundingBox getBoundingBox() {
		// TODO optimise this
		return new BoundingBox(polygonGenerator.generate(position, heading, 3, 10).getBounds2D());
	}

	public double getDirection() {
		return direction;
	}

	public double getHeading() {
		return heading;
	}

	public Laser getLaser() {
		return laser;
	}

	public Point2d getPosition() {
		return position;
	}

	public int getShotsFired() {
		return shotsFired;
	}

	public double getSpeed() {
		return speed;
	}

	public void laserHit() {
		laser.disable();
	}

	public void setDirection(double direction) {
		this.direction = direction;
	}

	public void setDisabled() {
	}

	public void setHeading(double heading) {
		this.heading = heading;
	}

	public void setSpeed(double speed) {
		this.speed = speed;
	}

	public void turnLeft() {
		movementManager.turnLeft();
	}

	public void turnRight() {
		movementManager.turnRight();
	}
}
